package com.huluobo.mvvm2003ademo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.mvvm2003ademo.databinding.ShopItemBinding;
import com.huluobo.mvvm2003ademo.model.Product;

/**
 * Created by lc on 2022/9/12.
 */
public class ShopListAdapter extends ListAdapter<Product, ShopListAdapter.ShopViewHolder> {

    ShopInterface shopInterface;

    public ShopListAdapter(ShopInterface shopInterface) {
        super(Product.itemCallback);
        this.shopInterface = shopInterface;
    }

    @NonNull
    @Override
    public ShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ShopItemBinding shopItemBinding = ShopItemBinding.inflate(layoutInflater, parent, false);
        shopItemBinding.setShopInterface(shopInterface);
        return new ShopViewHolder(shopItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ShopListAdapter.ShopViewHolder holder, int position) {
        Product product = getItem(position);
        holder.shopItemBinding.setProduct(product);
        holder.shopItemBinding.executePendingBindings();

    }

    class ShopViewHolder extends RecyclerView.ViewHolder {

        ShopItemBinding shopItemBinding;

        public ShopViewHolder(ShopItemBinding shopItemBinding) {
            super(shopItemBinding.getRoot());
            this.shopItemBinding = shopItemBinding;
        }
    }

    public interface ShopInterface {
        void addItem(Product product);

        void onItemClick(Product product);
    }
}
